{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 总述\n",
    "本教程记录Python和算法学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Collection库"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Counter\n",
    "输入一个list，输出一个字典，字典的key是数组元素，value是该元素在数组中出现的次数。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Counter({1: 2, 2: 1, 3: 1, 4: 1})"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import collections\n",
    "l = [1, 2, 3, 4, 1]\n",
    "counter = collections.Counter(l)\n",
    "counter"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 字典\n",
    "`dict.keys()`、`dict.values()` 和 `dict.items()` 返回的都是视图对象（ view objects），提供了字典实体的动态视图，这就意味着字典改变，视图也会跟着变化。\n",
    "视图对象不可被索引，可通过list将其转化为列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "(Counter({1: 2, 2: 1, 3: 2}),\n dict_keys([1, 2, 3]),\n dict_values([2, 1, 2]),\n dict_items([(1, 2), (2, 1), (3, 2)]))"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic = collections.Counter([1,2,1,3,3])\n",
    "dic, dic.keys(), dic.values(), dic.items()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "([(1, 2), (2, 1), (3, 2)], [(1, 2), (2, 1), (3, 2)])"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(dic.items()), sorted(dic.items())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}